﻿using System;
using System.Windows;
using Castle.Core.Logging;
using Castle.Windsor;
using SuppKids.Core.Extensions;
using SuppKids.IoC;
using SuppKids.Resources;

namespace SuppKids
{
	public partial class App : Application
	{
		private IWindsorContainer container;

		public IWindsorContainer Container
		{
			get
			{
				return this.container;
			}
		}

		protected override void OnStartup(StartupEventArgs e)
		{
			container = new WindsorContainer().RegisterComponents();
			container.Resolve<MainWindow>().Show();

			AppDomain.CurrentDomain.UnhandledException += this.OnUnhandledException;
		}

		private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
		{
			var exception = (Exception)e.ExceptionObject;
			var message = exception.GetDetailedMessage(Environment.NewLine);
			this.container.Resolve<ILogger>().Error(message, exception);

			if (MessageBox.Show(message, Messages.Error_Caption, MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification) == MessageBoxResult.OK)
			{
				Current.Shutdown();
			}
		}
	}
}
